package com.easthome.process.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easthome.model.process.ProcessTemplate;
import com.easthome.process.mapper.ProcessTemplateMapper;
import com.easthome.process.service.ProcessTemplateService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.List;
import java.util.zip.ZipInputStream;

/**
 * @description:
 * @author: 朱敦宇
 * @time: 2023/11/15 14:35
 */
@Service
public class ProcessTemplateServiceImpl extends ServiceImpl<ProcessTemplateMapper, ProcessTemplate> implements ProcessTemplateService {
    @Autowired
    private ProcessTemplateMapper processTemplateMapper;
    @Override
    public IPage<ProcessTemplate> selectPage(Page<ProcessTemplate> pageParam) {
        IPage<ProcessTemplate> all = processTemplateMapper.findAll(pageParam);



        return all;
    }


    @Autowired
    private RepositoryService repositoryService;
    @Override
    @Transactional
    public void publish(Long id) {
        ProcessTemplate processTemplate = this.getById(id);
        processTemplate.setStatus(1);
        processTemplateMapper.updateById(processTemplate);
//        repositoryService.createDeployment().name(processTemplate.getName()).deploy();
        InputStream inputStream = this
                .getClass()
                .getClassLoader()
                .getResourceAsStream(
                        processTemplate.getProcessDefinitionPath());
        ZipInputStream zipInputStream = new ZipInputStream(inputStream);
        // 流程部署
        Deployment deployment = repositoryService.createDeployment()
                .addZipInputStream(zipInputStream)
                .name(processTemplate.getProcessDefinitionKey())
                .deploy();
    }


}
